<template>
  <div class="site-category-edit">
    <!-- 编辑弹窗 -->
    <cvue-dialog
      :dialogVisible="dialogVisible"
      :title="title"
      :dialogLoading="dialogLoading"
      dialogWidth="500px"
      @handleOpen="getDetail"
      @closeDialog="closeDialog"
      @confirmDialog="confirmDialog"
      @handleClose="handleClose">
      <div class="dialog-body" slot="dialogBody">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="分类名称" prop="category">
                <el-input v-model="ruleForm.category" :disabled="disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="类型" prop="status">
                <el-radio-group v-model="ruleForm.status">
                  <el-radio :label="0">隐藏</el-radio>
                  <el-radio :label="1">显示</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="分类描述" prop="description">
                <el-input v-model="ruleForm.description" :disabled="disabled" type="textarea"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="排序号" prop="sort">
                <el-input v-model="ruleForm.sort" :disabled="disabled" type="number"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="上级分类" prop="pid">
                <el-select v-model="ruleForm.pid" placeholder="请选择" style="width:100%" clearable @change="changeSelect">
                  <el-option
                    v-for="item in categoryArr"
                    :key="item.id"
                    :label="item.category"
                    :disabled="item.disabled"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </cvue-dialog>
  </div>
</template>
<script>
import {mapGetters} from 'vuex'
import cvueDialog from '@/components/cvue-dialog'
import { validPhone, validIp, validIdCN, validInt } from '@/util/validator.js'
export default {
  name: 'site-category-edit',
  components: {
    cvueDialog
  },
  data () {
    return {
      ruleForm: {
        category: '',
        status: 1,
        sort: '',
        pid: ''
      },
      rules: {
        category: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序', trigger: 'blur' },
          { validator: validInt, trigger: 'blur' }
        ],
        pid: [
          { required: true, message: '请选择上级菜单', trigger: 'blur' }
        ]
      },
      disabled: false,
      dialogLoading: false,
      categoryArr: []
    }
  },
  mounted () {
    this.getCategorys()
    this.getEditDetail()
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    title () {
      var title = ''
      if (this.$parent.actionType == 'add') {
        title = '新增'
      } else {
        title = '编辑'
      }
      return title
    },
    ...mapGetters(['token', 'userInfo'])
  },
  methods: {
    // 触发事件
    handleClose () {
      this.$emit('handleClose')
    },
    closeDialog () {
      this.$emit('closeDialog')
    },
    confirmDialog () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          var params = {
            id: this.$parent.actionType == 'add' ? '' : this.$parent.rowData.id,
            pid: this.ruleForm.pid,
            category: this.ruleForm.category,
            sort: this.ruleForm.sort,
            status: this.ruleForm.status,
            description: this.ruleForm.description
          }
          // console.log(params)
          this.dialogLoading = true
          var url = this.$parent.actionType == 'add' ? 'blog/BlogSiteCategoryPost' : 'blog/BlogSiteCategoryPut'
          this.$store.dispatch(url, params).then(res => {
            // console.log(res)
            this.$message({
              message: res.msg,
              duration: 2000,
              type: 'success'
            })
            this.$emit('confirmDialog')
            this.dialogLoading = false
          }).catch(err => {
            this.$message({ message: err.msg, duration: 2000, type: 'error' })
            this.dialogLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getDetail () {
    },
    getCategorys () {
      var params = {
        status: ''
      }
      this.dialogLoading = true
      this.$store.dispatch('blog/BlogSiteCategoryGet', params).then(res => {
        // console.log(res)
        var arr = [
          {
            id: 0,
            category: '无'
          }
        ]
        if (res.data.length > 0) {
          if (this.$parent.actionType == 'edit') {
            res.data.forEach((item, index) => {
              if (item.id == this.$parent.rowData.id) {
                item.disabled = true
              }
            })
          }
          this.categoryArr = arr.concat(res.data)
          // console.log(this.categoryArr)
        } else {
          this.categoryArr = arr
        }
        this.dialogLoading = false
      })
    },
    changeSelect (value) {
      console.log(value)
    },
    getEditDetail () {
      if (this.$parent.actionType == 'edit') {
        this.$set(this.ruleForm, 'category', this.$parent.rowData.category)
        this.$set(this.ruleForm, 'status', this.$parent.rowData.status)
        this.$set(this.ruleForm, 'sort', this.$parent.rowData.sort)
        this.$set(this.ruleForm, 'pid', this.$parent.rowData.pid)
        this.$set(this.ruleForm, 'description', this.$parent.rowData.description)
      }
    }
  }
}
</script>
<style scoped>
</style>
